package model.classesDAO;

import java.util.List;

import javax.persistence.TypedQuery;

import model.classe.Ingrediente;
import model.interfaceDAO.IIngredienteDAO;

public class IngredienteDAO extends GenericoDAO<Ingrediente> implements IIngredienteDAO{

	@Override
	public void cadastrar(Ingrediente entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
		this.entityManager.persist(entidade);
		this.entityManager.getTransaction().commit();
		
	}

	@Override
	public void alterar(Ingrediente entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
		this.entityManager.persist(entidade);
		this.entityManager.getTransaction().commit();
		
	}

	@Override
	public void remover(Ingrediente entidade) {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
		this.entityManager.remove(entidade);
		this.entityManager.getTransaction().commit();
		
	}

	@Override
	public Ingrediente pesquisarID(Ingrediente entidade) {
		if(!this.entityManager.getTransaction().isActive());
		this.entityManager.getTransaction().begin();
		TypedQuery<Ingrediente> i = this.entityManager.createQuery("SELECT i FROM Ingrediente i WHERE id = " + entidade.getId(), Ingrediente.class);
		Ingrediente ingrediente = i.getSingleResult();
		return ingrediente;
		
	}

	@Override
	public List<Ingrediente> pesquisarTodos() {
		if(!this.entityManager.getTransaction().isActive())
			this.entityManager.getTransaction().begin();
		TypedQuery<Ingrediente> i = this.entityManager.createQuery("SELECT i FROM Ingrediente i ORDER BY nome ASC", Ingrediente.class);
		List<Ingrediente> ingredientes= i.getResultList();
		return ingredientes;
	}

}
